package com.yzx.exception;

import cn.hutool.http.HttpResponse;
import com.yzx.aspectj.OperLogAspect;
import com.yzx.vo.AjaxResult;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.resource.HttpResource;

import javax.servlet.http.HttpServlet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public AjaxResult jsonErrorHandler(MethodArgumentNotValidException e){
        List<Map<String, Object>> list = new ArrayList<>();
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        for (ObjectError allError : allErrors) {
            Map<String, Object> map =new HashMap<>();
            map.put("objectName",allError.getObjectName());
            map.put("defaultMessage",allError.getDefaultMessage());
            FieldError fieldError= (FieldError) allError;
            map.put("field",fieldError.getField());
            list.add(map);
        }
        return AjaxResult.fail("后端数据校验",list);
    }

}
